package com.chzc.fm.duotin.ui.channel;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.RankCommentAdapter;
import com.chzc.fm.duotin.bean.channel.Rank;
import com.chzc.fm.duotin.ui.other.BaseFragment;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class CommentMonthFragment extends BaseFragment {

    private RecyclerView recyclerView;

    private RankCommentAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_listen_today;
    }

    @Override
    protected void initViews() {

        recyclerView = (RecyclerView) root.findViewById(R.id.fragment_today_rc);

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

        getMonthComment(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {
                    LogUtil.w("开始解析数据");
                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if ("success".equals(error_msg))
                    {
                        LogUtil.w("解析成功");
                        JSONObject data = root.getJSONObject("data");
                        JSONArray datalsit = data.getJSONArray("data_list");

                        List<Rank> rankList = Rank.arrayRankFromData(datalsit.toString());

                        adapter = new RankCommentAdapter(getActivity(),rankList);
                        recyclerView.setAdapter(adapter);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });

    }

    /**
     * 获取今日榜单数据
     * @param callback
     */
    public void getMonthComment(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                return HttpUtil.doGet(GlobalParam.URL_COMMENT_MONTH);
            }
        };

        new DuotinTask(request,callback).execute();
    }
}
